#include "setting.h"


Setting::Setting()
{
    label_scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(label_scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_scr_load(label_scr);

    lv_style_init(&font_style);
	lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, LV_COLOR_LIME);

    lv_style_init(&font_style_msg);
	lv_style_set_text_font(&font_style_msg, LV_STATE_DEFAULT, &lv_font_montserrat_28);
	lv_style_set_text_color(&font_style_msg, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    label = lv_label_create(label_scr, NULL);
	lv_obj_add_style(label, LV_LABEL_PART_MAIN, &font_style);
	lv_label_set_text(label, LV_SYMBOL_SETTINGS " Setting\n   POKIUI\nVersion 1.2");
	lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_task_handler();
    save_cfg.begin("Config");
}

// save_cfg.putString("password", cmd_var);
void Setting::get_command()
{
    if(Serial.available()){
        is_input = true;
        char sin_byte;
        String comdata = "";
        while(1)
        {
            if (Serial.available()>0)
            {
                sin_byte = char(Serial.read());
                if(sin_byte=='\0')
                break;
                comdata += sin_byte;
            }
        }
        int pos1 = comdata.indexOf("#key#");
        int pos2 = comdata.indexOf("#value#");
        String key = comdata.substring(pos1+5, pos2);
        String value = comdata.substring(pos2+7, -1);
        
        save_cfg.putString(key.c_str(), value.c_str());
        msg += value + "\n";
        event = GREEN;
        last_event = event;
    }
    else if(last_event==GREEN)
    {
        event = RESET;
        last_event = NOEVENT;
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &font_style_msg);
        lv_label_set_text(label, msg.c_str());
        lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 15);
        lv_task_handler();
        msg = "";
    }

    
}

Setting::~Setting()
{
    lv_obj_del(label_scr);
}

Event Setting::main_process(Action action)
{
    event = NOEVENT;
    get_command();
    return event;
}